package com.xmindguoguo.boot.modular.system.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xmindguoguo.boot.core.BaseController;
import com.xmindguoguo.boot.modular.system.model.TSystemOperationLogModel;
import com.xmindguoguo.boot.modular.system.service.ITSystemOperationLogService;
import com.xmindguoguo.boot.util.ToolUtil;
import com.xmindguoguo.boot.util.date.DateUtil;
import com.xmindguoguo.boot.util.json.JsonResult;

/**
 * 控制器 //先暂时全部小写，后面写个方法注册进来，调整大小写
 * 
 * @author xmind-果果
 * @Date 2019-02-03 18:07
 */

@Controller
@RequestMapping("/tSystemOperationLog")
public class TSystemOperationLogController extends BaseController {

    // 所属子模块名称+当前功能名称
    private String PREFIX = "/system/tSystemOperationLog/";

    @Autowired
    private ITSystemOperationLogService systemOperationLogService;

    /**
     * 跳转列表页
     */
    @RequestMapping("toTSystemOperationLogList")
    public String toTSystemOperationLogList() {
        return PREFIX + "tSystemOperationLogList";
    }

    /**
     * 查询列表数据 表格 数据时用，不用删除
     */
    @RequestMapping("tSystemOperationLogList")
    @ResponseBody
    public Object tSystemOperationLogList(TSystemOperationLogModel model, @RequestParam(value = "page", required = false) Integer current,
            @RequestParam(value = "limit", required = false) Integer size, @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        Wrapper<TSystemOperationLogModel> wrapper = new EntityWrapper<>();
        if (ToolUtil.isNotEmpty(model.getLogName())) {
            wrapper.like("logName", model.getLogName());
        }
        if (ToolUtil.isNotEmpty(model.getLogType())) {
            wrapper.eq("logType", model.getLogType());
        }
        if (ToolUtil.isNotEmpty(model.getIp())) {
            wrapper.eq("ip", model.getIp());
        }
        if (ToolUtil.isNotEmpty(model.getAddress())) {
            wrapper.like("address", model.getAddress());
        }
        if (StringUtils.isNotBlank(startTime)) {
            wrapper.ge("createTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            wrapper.lt("createTime", DateUtils.addDays(DateUtil.str2Date(endTime), 1));
        }
        wrapper.orderBy("createTime", false);
        Page<TSystemOperationLogModel> page = new Page<>(current, size);
        return JsonResult.sendPage(systemOperationLogService.selectPage(page, wrapper)); // 跳转到列表页
    }

    @RequestMapping("tSystemOperationLogDetail")
    public String tSystemOperationLogDetail(Long id) {
        TSystemOperationLogModel model = systemOperationLogService.selectById(id);
        // model 自动转换为浏览器对应的类型，可以安全的提示
        request.setAttribute("model", model);
        return PREFIX + "tSystemOperationLogDetail";
    }

}